package com.example.demo.auth.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.annotation.ApiDesc;
import com.example.demo.auth.service.LoginService;
import com.example.demo.entity.LoginEnity;
import com.example.demo.entity.base.ResponseEntity;
import com.example.demo.util.TokenUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping(value="/auth/login")
public class LoginController {
	
	@Autowired
	private LoginService loginService;
	
	/**
	 * 登陆系统
	 * @param request
	 * @param logininfo
	 * @return
	 */
	@ApiDesc("登陆系统")
	@RequestMapping(value="/logon",method=RequestMethod.POST)
	public Object logOn(HttpServletRequest request,@RequestBody LoginEnity logininfo,HttpServletResponse response) {
		
		String token = TokenUtil.getTokenFromRequest(request);
		
		logininfo.setToken(token);
		
		ResponseEntity<String> ret = loginService.logOn(logininfo,response,request);
		
		return ret;		
	}
	
	/**
	 * @return
	 * @创建时间: 2019年8月30日
	 * @创建人: 许子文
	 * @功能描述: 登出,清除登陆状态
	 */
	@ApiDesc("登出系统")
	@RequestMapping(value="/logout")
	public Object logOut(HttpServletRequest request,HttpServletResponse response) {
		
		String token = TokenUtil.getTokenFromRequest(request);
		
		ResponseEntity<String> ret = loginService.logOut(token,response);
		
		return ret;		
	}
	
	@ApiDesc("获取密钥对")
	@RequestMapping(value="/getrsapubkey")
	public ResponseEntity<String> getRsaPubKey(HttpServletRequest request){
		
		ResponseEntity<String> ret = loginService.getRsaPubKey();
		
		return ret;				
	}
}
